package com.goktech.sportsmall.controller;

import com.goktech.sportsmall.bean.LoginBean;
import com.goktech.sportsmall.bean.UserBean;
import com.goktech.sportsmall.service.LoginService;
import com.goktech.sportsmall.service.UserService;
import com.goktech.sportsmall.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class VueUserController {

    @Autowired
    private UserService userService;
    @Autowired
    private LoginService loginService;

    // 忘记密码
    @RequestMapping(value = "/forget",method = RequestMethod.GET)
    public Map<String,Object> forgetPassword(@RequestParam("name") String name,
                                             @RequestParam("tel") String tel,
                                             @RequestParam("code") String code) {
        Map<String,Object> hashMap = new HashMap<>();
        // 检验是否存在账号信息
        UserBean userBean=loginService.forget(name,tel);
        hashMap.put("title","忘记密码");
        if (userBean == null) {
            hashMap.put("message","用户信息不存在");
            return hashMap;
        }
        hashMap.put("message","存在账号信息");
        return hashMap;
    }

    // 重置密码
    @RequestMapping(value = "/reset",method = RequestMethod.GET)
    public Map<String,Object> resetPassword(@RequestParam("tel") String tel,
                                            @RequestParam("pwd") String pwd){
        Map<String,Object> hashMap = new HashMap<>();
        Map<String,Object> hash = new HashMap<>();
        UserBean userBean = userService.selectUserPhoneInfo(tel);
        userBean.getU_id(); // 获取账号信息
        // 重置密码
        String reset = loginService.reset(pwd, userBean.getU_id());
        hash.put("tel",tel);
        hash.put("pwd",pwd);
        hashMap.put("data",hash);
        hashMap.put("message",reset);
        hashMap.put("time", DateUtil.getDate("yyyy/MM/dd HH:mm:ss"));
        return hashMap;
    }

    // 用户登录
    @RequestMapping(value = "loginUser",method = RequestMethod.GET)
    public Map<String,Object> loginUser(@RequestParam("tel") String tel,
                                        @RequestParam("pwd") String pwd) {

        Map<String,Object> hashMap = new HashMap<>();
        hashMap.put("title","用户登录");
        UserBean userBean=loginService.loginUser(tel,pwd);
        if (userBean==null){
            hashMap.put("message","登录失败");
            return hashMap;
        }
        hashMap.put("message","登录成功");
        hashMap.put("data",userBean);
        return hashMap;
    }

    // 用户注册
    @RequestMapping(value = "register",method = RequestMethod.GET)
    public Map<String,Object> registerPassword( String name,
                                                String tel,
                                                String pwd) {
        // 进行判断注册的账号是否存在
        int telCount = userService.selectUserPhone(tel);
        Map<String,Object> hashMap = new HashMap<>();
        Map<String,Object> hash = new HashMap<>();
        hashMap.put("title","用户注册");
        hash.put("tel",tel);
        hashMap.put("time", DateUtil.getDate("yyyy/MM/dd HH:mm:ss"));
        if (telCount == 1) {
            hashMap.put("message","账号已经存在");
            return hashMap;
        } else {
            // 进行用户注册
            UserBean bean = new UserBean();
            bean.setuPhone(tel);
            bean.setuName(name);
            bean.setuCreateTime(DateUtil.getDate("yyyyMMdd"));
            userService.insertUser(bean);

            // 查找注册成功的编号
            UserBean userBeans = userService.selectUserPhoneInfo(tel);
            LoginBean loginBean = new LoginBean();
            loginBean.setAluId(userBeans.getU_id());
            loginBean.setlPassword(pwd);
            loginBean.setlAccount(name);
            loginBean.setlType(1);
            loginBean.setlCreateTime(DateUtil.getDate("yyyy/MM/dd HH:mm:ss"));
            int loginTip = loginService.insertUser(loginBean);
            if (loginTip == 1) {
                hashMap.put("message", "注册成功");
                hash.put("password", pwd);
                hash.put("name", name);
            }
            hashMap.put("data", hash);
        }
        return hashMap;
    }

    @RequestMapping(value = "updateInfo",method = RequestMethod.GET)
    public Map<String,Object> updateInfo(UserBean userBean) {
        Map<String,Object> hashMap = new HashMap<>();
        hashMap.put("title","修改个人信息");
        hashMap.put("message",userService.updateUser(userBean));
        hashMap.put("data",userBean);
        return hashMap;
    }

}
